import json
from bazy.models import User
from django.http import HttpResponse
import hashlib
from bazy.login import Login
from bazy.forms import AddUserForm
from django.db import IntegrityError


def getAll(request):
	log = Login(request);
	if log.isLogged() is not True:
		return HttpResponse(json.dumps([{'Error':-1}]))
	userArray = User.objects.all()
	jsonList = list()
	for usr in userArray:
		jsonList.append({'id':usr.id,
						 'name':usr.name,
						 'lastname':usr.lastname,
						 'username':usr.username,
						 'password':usr.password,
						 'address':usr.address,
						 'cardNumber':usr.cardNumber,
						 'phoneNumber':usr.phoneNumber,
						 'privilage':usr.privilage, })
#   print json.dumps(jsonList)
	return HttpResponse(json.dumps(jsonList))

def getById(request):
	log = Login(request);
	if log.isLogged() is not True:
		return HttpResponse(json.dumps([{'Error':-1}]))
	if request.method != 'POST':
		return HttpResponse(json.dumps([{'Error':-2}]))
	id = request.POST.get('id', -1)
	if id == -1:
		return HttpResponse(json.dumps([{'Error':-2}]))
	
	usr = User.objects.get(pk=id)
	return HttpResponse(json.dumps([{'id':usr.id,
						 'name':usr.name,
						 'lastname':usr.lastname,
						 'username':usr.username,
						 'password':usr.password,
						 'address':usr.address,
						 'cardNumber':usr.cardNumber,
						 'phoneNumber':usr.phoneNumber,
						 'privilage':usr.privilage, }]))
def setById(request):
	log = Login(request);
	if log.isLogged() is not True:
		return HttpResponse(json.dumps([{'Error':-1}]))
	if request.method != 'POST':
		return HttpResponse(json.dumps([{'Error':-2}]))
	id = request.POST.get('id', -1)
	if id == -1:
		return HttpResponse(json.dumps([{'Error':-2}]))
	user= User.objects.get(pk=id)
	user.name = request.POST['name']
	user.lastname = request.POST['lastname']
	user.username = request.POST['username']
	password = request.POST['password']
	rePassword = request.POST['rePassword']
	if password!=rePassword:
		return HttpResponse(json.dumps([{'Error':-2}])) 
	user.address = request.POST['address']
	user.cardNumber = request.POST['cardNumber']
	user.phoneNumber = request.POST['phoneNumber']
	user.privilage = request.POST['privilage']
	if len(password)>0:
		user.password = hashlib.sha1(password).hexdigest()
	user.save()	
	return HttpResponse(json.dumps([{'Error':0}]))
def deleteById(request):
	log = Login(request);
	if log.isLogged() is not True:
		return HttpResponse(json.dumps([{'Error':-1}]))
	if request.method != 'POST':
		return HttpResponse(json.dumps(False))
	id = request.POST.get('id', -1)
	if id == -1:
		return HttpResponse(json.dumps([{'Error':-2}]))
	user = User.objects.get(pk=id)
	user.delete()
	return HttpResponse(json.dumps([{'Error':0}]))
	
def add(request):
	log = Login(request);
	if log.isLogged() is not True:
		return HttpResponse(json.dumps([{'Error':-1}]))
	if request.method != 'POST':
		return HttpResponse(json.dumps([{'Error':-2}]))
	form = AddUserForm(request.POST)
	if not form.is_valid():
		return HttpResponse(json.dumps([{'Error':-2}])) 
	name = request.POST['name']
	lastname = request.POST['lastname']
	username = request.POST['username']
	password = request.POST['password']
	rePassword = request.POST['rePassword']
	
	if password!=rePassword:
		return HttpResponse(json.dumps([{'Error':-2}])) 
	address = request.POST['address']
	cardNumber = request.POST['cardNumber']
	phoneNumber = request.POST['phoneNumber']
	privilage = request.POST['privilage']
	 
	password = hashlib.sha1(password).hexdigest();
	user = User(name=name, lastname=lastname, username=username, password=password, address=address, cardNumber=cardNumber, privilage=privilage, phoneNumber=phoneNumber)
	try:
		user.save()
	except IntegrityError:
		return HttpResponse(json.dumps([{'Error':-3}]))
			
	return HttpResponse(json.dumps([{'Error':0}]))

